package com.xx.bysj.service.imp;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xx.bysj.dto.HnOrder;
import com.xx.bysj.dto.HnUser;
import com.xx.bysj.mapper.HnOrderMapper;
import com.xx.bysj.mapper.HnUserMapper;
import com.xx.bysj.service.AssignService;
import com.xx.bysj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(rollbackFor = Exception.class)
public class AssignServiceImpl implements AssignService {

    @Autowired
    HnUserMapper userMapper;

    @Autowired
    HnOrderMapper orderMapper;

    @Override
    public Result query(HnOrder order, Authentication authentication, int page, int limit) {
        HnUser dto = new HnUser();
        dto.setUserName(authentication.getName());
        HnUser user = userMapper.selectByUserName(dto);
        if (user.getUserRoleId() != 0) {
            order.setOrderUserId(user.getUserId());
        }
        PageHelper.startPage(page,limit);
        List<HnOrder> orderList = orderMapper.selectToAssign(order);
        PageInfo<HnOrder> pageInfo = new PageInfo<>(orderList);
        return Result.querySuccess(orderList,(int)pageInfo.getTotal());
    }

    @Override
    public Result update(Authentication authentication, List<Integer> orderIds, Integer userId) {

        if (orderIds.size() <= 0) {
            return Result.fail("请勾选订单");
        }

        if (null == userId) {
            return Result.fail("请勾选员工");
        }

        for (int i = 0; i < orderIds.size(); i++) {
            Integer id = orderIds.get(i);
            orderMapper.updateByUserId(userId,id);
        }

        return Result.success("分配成功");
    }
}
